import { Callback, IComponent } from "./base/IComponent";

class _Canvas extends IComponent {
    public static id: number = 0;
    private _element: HTMLCanvasElement = document.createElement("canvas");
    private readonly context: CanvasRenderingContext2D | null = null;

    constructor(callback?: Callback<HTMLCanvasElement>) {
        super(`canvas-${_Canvas.id++}`);
        this._element.id = this._id;
        this.context = this._element.getContext("2d");
        console.log('Canvas constructed')
        if (callback) {
            callback(this._element);
        }
    }

    build(_parentId?: string) {
        super._build(this._element, _parentId);
        this._onContext(this.context);
    }

    onDidBuild(callback: (context: CanvasRenderingContext2D | null) => void) {
        this._onContext = callback;
        return this;
    }

    private _onContext: (context: CanvasRenderingContext2D | null) => void = () => undefined;
}

export function Canvas(callback?: Callback<HTMLCanvasElement>) {
    return new _Canvas(callback);
}